package com.fbinggun.fbinggunutil.plugin.nbt;

import de.tr7zw.nbtapi.NBTCompound;
import de.tr7zw.nbtapi.NBTEntity;
import org.bukkit.entity.Entity;
/**
 * NBTAPI 插件EntityNBT API
 */
public class EntityNBT {


    public static String getString(String key, Entity entity){
        NBTEntity nbt = new NBTEntity(entity);
        System.out.println(nbt.getCompound());
        if(key.contains(".")){
            String[] args = key.split(".");
            if(args.length==1){
              return   nbt.getString(key);
            }else {
              NBTCompound nbtCompound=nbt.getCompound(args[0]);
              if(nbtCompound==null){
                  return null;
              }
                for(int x =0;x<=args.length;x++){
                    if(x==0){
                        continue;
                    }
                    String k = args[x];
                    if(k.equals(args[args.length-1])) {
                      return  nbtCompound.getString(args[args.length-1]);
                    }
                    nbtCompound=nbtCompound.getCompound(args[x]);
                }
                return null;
            }
        }else {
            System.out.println(nbt.toString());
            return nbt.getString(key);
        }
    }

    public static Double getDouble(String key, Entity entity){
        NBTEntity nbt = new NBTEntity(entity);
        if(key.contains(".")){
            String[] args = key.split(".");
            if(args.length==1){
                return   nbt.getDouble(key);
            }else {
                NBTCompound nbtCompound=nbt.getCompound(args[0]);
                if(nbtCompound==null){
                    return 0d;
                }
                for(int x =0;x<=args.length;x++){
                    if(x==0){
                        continue;
                    }
                    String k = args[x];
                    if(k.equals(args[args.length-1])) {
                        return  nbtCompound.getDouble(args[args.length-1]);
                    }
                    nbtCompound=nbtCompound.getCompound(args[x]);
                }
                return 0d;
            }
        }else {
            return nbt.getDouble(key);
        }
    }
    public static int getInt(String key, Entity entity){
        NBTEntity nbt = new NBTEntity(entity);
        if(key.contains(".")){
            String[] args = key.split(".");
            if(args.length==1){
                return   nbt.getInteger(key);
            }else {
                NBTCompound nbtCompound=nbt.getCompound(args[0]);
                if(nbtCompound==null){
                    return 0;
                }
                for(int x =0;x<=args.length;x++){
                    if(x==0){
                        continue;
                    }
                    String k = args[x];
                    if(k.equals(args[args.length-1])) {
                        return  nbtCompound.getInteger(args[args.length-1]);
                    }
                    nbtCompound=nbtCompound.getCompound(args[x]);
                }
                return 0;
            }
        }else {
            return nbt.getInteger(key);
        }
    }
    public static Long getLong(String key, Entity entity){
        NBTEntity nbt = new NBTEntity(entity);
        if(key.contains(".")){
            String[] args = key.split(".");
            if(args.length==1){
                return   nbt.getLong(key);
            }else {
                NBTCompound nbtCompound=nbt.getCompound(args[0]);
                if(nbtCompound==null){
                    return 0l;
                }
                for(int x =0;x<=args.length;x++){
                    if(x==0){
                        continue;
                    }
                    String k = args[x];
                    if(k.equals(args[args.length-1])) {
                        return  nbtCompound.getLong(args[args.length-1]);
                    }
                    nbtCompound=nbtCompound.getCompound(args[x]);
                }
                return 0l;
            }
        }else {
            return nbt.getLong(key);
        }
    }
}
